// GeneralEdit.cpp : implementation file
//

#include "stdafx.h"
#include "a1.h"
#include "GeneralEdit.h"
#include "DlgXSDJ.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CGeneralEdit

CGeneralEdit::CGeneralEdit()
{	m_poplist=NULL;
	m_bSetValue=false;
	m_Kill=NORMALKILL;
	m_bSetFocus=true;
}

CGeneralEdit::~CGeneralEdit()
{
}


BEGIN_MESSAGE_MAP(CGeneralEdit, CEdit)
	//{{AFX_MSG_MAP(CGeneralEdit)
	ON_WM_SETFOCUS()
	ON_WM_KILLFOCUS()
	ON_CONTROL_REFLECT(EN_CHANGE, OnChange)
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
	//ON_MESSAGE(MYMESSAGE_EDIT_KHID_MOVED,OnParentMoved)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGeneralEdit message handlers
BOOL CGeneralEdit::PreTranslateMessage(MSG* pMsg) 
{
	// TODO: Add your specialized code here and/or call the base class
	if(pMsg->message==WM_KEYDOWN)
	{
		switch(pMsg->wParam)
		{
		case 13:
		{
			pMsg->message=WM_KILLFOCUS;
			m_Kill=RETURNKILL;
			break;
		}
		case 27:
		{
			if(m_poplist->IsWindowVisible())
			{
			this->m_poplist->ShowWindow(false);
			pMsg->wParam=VK_CONTROL;
			}

			break;
		}
		case VK_UP:
		case VK_DOWN:
		case VK_NEXT:
		case VK_PRIOR:
			{
				this->m_poplist->SendTheUDNPMessage(pMsg->wParam);
				pMsg->wParam=VK_CONTROL;
				break;
			}
		}
	}

	return CEdit::PreTranslateMessage(pMsg);
}

void CGeneralEdit::OnSetFocus(CWnd* pOldWnd) 
{

	ASSERT(this->m_poplist!=NULL);
	m_poplist->ShowWindow(SW_HIDE);

	CEdit::OnSetFocus(pOldWnd);
}

void CGeneralEdit::OnKillFocus(CWnd* pNewWnd) 
{	
	CEdit::OnKillFocus(pNewWnd);

	int iID=this->GetDlgCtrlID();
	switch(m_Kill)
	{
	case RETURNKILL:
		{

			ASSERT(this->m_poplist!=NULL);
			if(m_poplist->IsWindowVisible()&&m_poplist->GetSelectedMark()>=0)
			{				
				this->m_poplist->ShowWindow(SW_HIDE);	
				CGeneralEdit* edit;
				edit=(CGeneralEdit* )this->GetParent()->GetDlgItem(IDC_EDIT_KHNAME);
				edit->SetValueUnOnChange(m_poplist->GetListCtrlSel(1));
				edit->Invalidate();

				edit=(CGeneralEdit* )this->GetParent()->GetDlgItem(IDC_EDIT_KHID);
				edit->SetValueUnOnChange(m_poplist->GetListCtrlSel(0));
				edit->Invalidate();
			}
			this->m_poplist->ShowWindow(SW_HIDE);	
			::PostMessage(this->GetParent()->GetParent()->GetSafeHwnd(),MYMESSAGE_XSDJ_SETTHEFOCUS,0,0);
			break;
		}
	default:
		if(pNewWnd!=NULL)
		if(pNewWnd->GetDlgCtrlID()==m_poplist->GetDlgCtrlID()||
			pNewWnd->GetDlgCtrlID()==ID_POP_LISTCTRL);
		else this->m_poplist->ShowWindow(SW_HIDE);

		break;
	}
	m_Kill=NORMALKILL;
	// TODO: Add your message handler code here
}

void CGeneralEdit::OnChange() 
{
	if(m_bSetValue) return;
	ASSERT(this->m_poplist!=NULL);
	CString str;
	this->GetWindowText(str);
	int iID=this->GetDlgCtrlID();

		if(iID==IDC_EDIT_KHID)
		{
		this->m_poplist->RefreshListCtrlView(0,str);

		}
		else if(iID==IDC_EDIT_KHNAME)
		{
			CString id;
			CWnd * editid;
			editid=this->GetParent()->GetDlgItem(IDC_EDIT_KHID);
			editid->GetWindowText(id);
			m_poplist->RefreshListCtrlView(1,str,id,0);
		}
	if(!m_poplist->IsWindowVisible())
	{
		this->GetWindowRect(&m_rectWindow);
		this->m_poplist->SetShowPosition(this->m_rectWindow,this);
		m_poplist->ShowListPop();
	}
}
void CGeneralEdit::SelectPopList(CListCtrlPop *poplist)
{
	ASSERT(poplist!=NULL);
	this->m_poplist=poplist;
}

void CGeneralEdit::OnParentMoved()
{
	this->GetWindowRect(&this->m_rectWindow);
	if(this->m_poplist->IsWindowVisible()&&this==this->m_poplist->GetCurEditWnd())
	{
		this->m_poplist->SetShowPosition(m_rectWindow,this);
		this->m_poplist->ShowListPop();
	}
}

void CGeneralEdit::SetValueUnOnChange(CString text)
{
	this->m_bSetValue=true;
	this->SetWindowText(text);
	this->m_bSetValue=false;
}

void CGeneralEdit::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	CRect rect;
	this->GetClientRect(&rect);
	dc.FillSolidRect(&rect,RGB(254,254,254));
	dc.MoveTo(rect.left,rect.bottom-8);
	dc.LineTo(rect.right,rect.bottom-8);
	
	CString str;
	this->GetWindowText(str);
	CFont* font=this->GetFont();
	dc.SelectObject(font);
	dc.TextOut(0,0,str);
}
